
#include <iostream>
#include <string>
#include <thread>
#include <atomic>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <sys/wait.h>
#include <array>
#include <vector>

#include "TextRenderer.h"

// 全局原子标志，用于控制线程退出
std::atomic<bool> should_exit(false);

// 执行命令并返回管道文件描述符
std::vector<int> execute_command(const char* cmd) {
    int pipefd[2];
    if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    pid_t pid = fork();
    if (pid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid == 0) { // 子进程开启失败
        close(pipefd[0]);    // 关闭读端
        dup2(pipefd[1], 1);  // 将标准输出重定向到管道写端
        dup2(pipefd[1], 2);  // 将标准错误也重定向到管道写端
        close(pipefd[1]);

        execl("/bin/sh", "sh", "-c", cmd, (char*)NULL);
        perror("execl");
        exit(EXIT_FAILURE);
    }
    cout << "输出pipe:" <<pipefd[0]<<pipefd[1]<< std::endl;
    return {pipefd[0],pipefd[1]}; // 返回管道
}

int print_numbers() {
    int font_size = 24;
    uint16_t color = 0xFFFF; // 白色
    int x = 0;
    int y = 0;
    bool contain = true;
    TextRenderer::Orientation orientation = TextRenderer::Orientation::Horizontal;
    TextRenderer renderer;
    renderer.init_frame();

    // 执行 bluetoothctl 命令
    std::vector<int> pipefd = execute_command("bluetoothctl");
    close(pipefd[1]);// 关闭读端
    FILE* pipe_fp = fdopen(pipefd[0], "r");
    if (!pipe_fp) {
        std::cout << "io打开失败" << std::endl;
        perror("fdopen");
        return EXIT_FAILURE;
    }

    char* line = nullptr;
    size_t len = 0;
    const std::string title_prefix = "Track.Title: ";

    while (!should_exit) {
        std::cout << "应该输出歌词" << std::endl;
        if (getline(&line, &len, pipe_fp) == -1) {
            if (feof(pipe_fp)) {
                break; // 命令结束
            }
            perror("getline");
            break;
        }
        std::string line_str(line);
        std::cout << "line " <<line_str<< std::endl;
        size_t title_pos = line_str.find(title_prefix);
        if (title_pos != std::string::npos) {
            std::string title = line_str.substr(title_pos + title_prefix.length());
            // 移除可能的换行符
            if (!title.empty() && title.back() == '\n') {
                title.pop_back();
            }
            //这里处理每一句歌词
            std::cout << "当前歌词: " << title << std::endl;
            renderer.render_text(title, x, y, color, font_size, contain, orientation);
            renderer.display();
        }
    }
    close(pipefd[0]);// 关闭写端
    return EXIT_SUCCESS;
}
int exitq() {

    std::string input;
    while (!should_exit) {
        std::cout << "Press 'q' + Enter to exit..." << std::endl;
        sleep(12);
        std::getline(std::cin, input);
        if (input == "q") {
            should_exit = true;
            break;
        }
    }
    std::cout << "Input thread exiting..." << std::endl;
    return EXIT_SUCCESS;
}
int main() {
    // 输出线程：每秒打印数字
    print_numbers();

    // 输入线程：检测用户输入
    // std::thread input_thread(exitq);

    // 等待线程结束
    // std::cout << "output_thread start" << std::endl;
    // output_thread.join();
    std::cout << "input_thread start" << std::endl;
    // input_thread.join();

    std::cout << "Program exited cleanly." << std::endl;
    return 0;
}